<template>
  <div class="subject">
    <el-card>
      <el-form ref="form" :model="form" :inline="true">
        <el-form-item ::label="$t('route.EnterpriseList')">
          <el-input v-model.trim="form.rid" />
        </el-form-item>
        <el-form-item :label="$t('route.Subjectname')">
          <el-input v-model.trim="form.name" />
        </el-form-item>
        <el-form-item :label="$t('route.SubjectCreator')">
          <el-input v-model.trim="form.username" />
        </el-form-item>
        <el-form-item :label="$t('route.state')">
          <el-select
            v-model="form.status"
            :placeholder="$t('route.Pleaseselect')"
          >
            <el-option
              v-for="item in subjectData.stateType"
              :key="item.id"
              :label="item.value"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="searchEvent">{{
            $t('route.search')
          }}</el-button>
          <el-button @click="clearEvent">{{ $t('route.eliminate') }}</el-button>
          <el-button type="primary" @click="addEvent">
            <i class="iAdd el-icon-plus" />
            {{ $t('route.Newdisciplines') }}</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card>
      <!-- el-table -->
      <el-table
        :data="list"
        :cell-style="setCellStyle"
        :header-cell-style="{ color: '#909399' }"
      >
        <el-table-column
          :label="$t('route.SerialNo')"
          type="index"
          width="88px"
        >
          <template v-slot="{ $index }">
            <div>
              {{ (form.page - 1) * form.limit + ($index + 1) }}
            </div>
          </template>
        </el-table-column>
        <el-table-column :label="$t('route.SubjectNo')" prop="rid" />
        <el-table-column :label="$t('route.Subjectname')" prop="name" />
        <el-table-column
          :label="$t('route.AbbreviationofSubject')"
          prop="short_name"
        />
        <el-table-column :label="$t('route.creator')" prop="username" />
        <el-table-column :label="$t('route.CreationDate')" prop="create_time" />
        <el-table-column
          class="active"
          :label="$t('route.state')"
          prop="status"
          :formatter="formatterState"
        />
        <el-table-column :label="$t('route.operation')">
          <template v-slot="{ row }">
            <div>
              <el-button type="text" @click="editEvent(row)">{{
                $t('route.edit')
              }}</el-button>
              <el-button type="text" @click="stopEvent(row)">{{
                row.status === 0 ? $t('route.startUp') : $t('route.Disable')
              }}</el-button>
              <el-button type="text" @click="delClick(row)">
                {{ $t('route.delete') }}</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="page">
        <el-pagination
          :current-page.sync="form.page"
          :page-sizes="[1, 2, 5, 10, 20, 30, 50, 100]"
          :page-size.sync="form.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="sizeChange"
          @current-change="currentChange"
        />
      </div>
    </el-card>

    <!-- Add -->
    <Add
      ref="add"
      :init-list="initList"
      @getsubjectlist="getSubjectList"
      @getsubjectlist2="getSubjectList2"
    />
  </div>
</template>

<script>
// 导入
import jsCookie from 'js-cookie'
import { subjectList, subjectRemove, subjectStatus } from '@/api/subject.js'

// 枚举
import subjectData from '@/api/contant/subject.js'
// add
import Add from '@/views/Subject/components/add.vue'
export default {
  // 注册组件
  components: {
    Add
  },
  // 数据
  data() {
    return {
      list: [],
      initList: [],
      form: {
        rid: '', //	否	string	学科编号
        name: '', //	否	string	学科名称
        username: '', //	否	string	创建者用户名
        page: +'1', //	否	string	页码 默认为1
        limit: +jsCookie.get('sub-cookie') || +'2', //	否	string	页尺寸 默认为10
        status: '' //	否	string	状态 0(禁用) 1(启用)
      },
      total: 100,
      subjectData: subjectData
    }
  },
  created() {
    this.getSubjectList()
    this.getSubjectList2()
  },
  // 方法的集合
  methods: {
    // 获取学科列表
    async getSubjectList() {
      const res = await subjectList(this.form)
      this.list = res.data.data.items
      // 设置总数
      this.total = res.data.data.pagination.total
      console.log(res)
      console.log(this.list)
    },
    async getSubjectList2() {
      const res = await subjectList({
        rid: '', //	否	string	学科编号
        name: '', //	否	string	学科名称
        username: '', //	否	string	创建者用户名
        page: +'1', //	否	string	页码 默认为1
        limit: +'9999', //	否	string	页尺寸 默认为10
        status: '' //	否	string	状态 0(禁用) 1(启用)
      })

      this.initList = res.data.data.items
    },
    // currentChange，当前页改变时
    currentChange() {
      this.getSubjectList()
    },
    // sizeChange，页容量改变时
    sizeChange() {
      this.getSubjectList()
      // 页容量的持久化
      jsCookie.set('sub-cookie', this.form.limit)
      this.form.page = 1
    },
    // formatterState，状态
    formatterState(row, column, cellValue, index) {
      const findItems = subjectData.stateType.find(item => {
        return item.id === +cellValue
      })
      return findItems ? findItems.value : '-'
    },
    // setCellStyle，状态
    setCellStyle({ row, column, rowIndex, columnIndex }) {
      if (row.status === 0 && columnIndex === 6) {
        return 'color:red'
      } else {
        return 'color:#606266'
      }
    },
    // 新增学科
    addEvent() {
      this.$refs.add.isShow = true
      this.$refs.add.mode = 'add'
    },
    // 编辑学科
    editEvent(row) {
      this.$refs.add.isShow = true
      this.$refs.add.mode = 'edit'
      // 回显数据
      this.$refs.add.form = JSON.parse(JSON.stringify(row))
      this.$refs.add.form.rid = row.rid
      console.log(row)
    },
    // 删除学科
    delClick(row) {
      this.$confirm('您确定删除此学科吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await subjectRemove(row)
          if (this.form.page > 1 && this.list.length === 1) {
            this.form.page--
          }
          // 提示
          this.$message.success('删除成功')
          // 调用查询方法
          this.getSubjectList()
        })
        .catch(() => {})
    },
    // 禁用
    async stopEvent(row) {
      await subjectStatus(row)
      // 调用查询
      this.getSubjectList()
    },
    // 搜索
    async searchEvent() {
      const res = await subjectList(this.form)
      this.list = res.data.data.items
      // 设置总数
      this.total = res.data.data.pagination.total

      this.form.page = 1
    },
    // 清空
    clearEvent() {
      this.form = {
        rid: '', //	否	string	学科编号
        name: '', //	否	string	学科名称
        username: '', //	否	string	创建者用户名
        page: +'1', //	否	string	页码 默认为1
        limit: +jsCookie.get('sub-cookie') || +'2', //	否	string	页尺寸 默认为10
        status: '' //	否	string	状态 0(禁用) 1(启用)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.subject {
  padding: 20px;
  // height: 100vh;
  background-color: #cbcbcb;

  .iAdd {
    font-size: 12px;
    font-weight: bold;
  }
  .page {
    text-align: center;
    padding: 20px 0px 0px;
  }
}
::v-deep .el-form-item {
  .el-form-item__label {
    color: #606266;
    font-weight: normal;
  }
}
</style>
